﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test1
{
    class Program
    {
        private static double num1 = 0, num2 = 0, num3 = 0;
        private static string str = null;
        static void menu()
        {
            Console.Clear();
            Console.WriteLine("====进行加减乘除和求余运算====");
            Console.WriteLine("       + -- 加法");
            Console.WriteLine("       - -- 减法");
            Console.WriteLine("       * -- 乘法");
            Console.WriteLine("       / -- 除法");
            Console.WriteLine("       % -- 求余");
            Console.WriteLine("       0 -- 退出");
        }
        static void canshu1()
        {
            try
            {
                Console.WriteLine("请输入第一个参数:");
                num1 = double.Parse(Console.ReadLine());
            }
            catch (Exception e)
            {
                Console.WriteLine("您输入的有错，请重新输入.");
                canshu1();
            }
        }
        static void canshu2()
        {
            try
            {
                Console.WriteLine("请输入第二个参数:");
                num2 = double.Parse(Console.ReadLine());
            }
            catch (Exception e)
            {
                Console.WriteLine("您输入的有错，请重新输入.");
                canshu2();
            }
        }
        static void jisuan(string choice)
        {
            switch (choice)
            {
                case "+":
                    num3 = num1 + num2;
                    break;
                case "-":
                    num3 = num1 - num2;
                    break;
                case "/":
                    try
                    {
                        if (num2 == 0)
                        {
                            throw new DivideByZeroException();
                        }
                        else
                        {
                            num3 = num1 / num2;
                        }
                    }
                    catch (DivideByZeroException)
                    {
                        Console.WriteLine("除数不能为零");
                        Console.WriteLine("按回车继续......");
                        Console.ReadLine();
                        zhixing();
                    }
                    break;
                case "*":
                    num3 = num1 * num2;
                    break;

                case "%":
                    try
                    {
                        if (num2 == 0)
                        {
                            throw new DivideByZeroException();
                        }
                        else
                        {
                            num3 = num1 % num2;
                        }
                    }
                    catch (DivideByZeroException)
                    {
                        Console.WriteLine("除数不能为零");
                        Console.WriteLine("按回车继续......");
                        Console.ReadLine();
                        zhixing();
                    }
                    break;
            }
        }
        static void zhixing()
        {
            while (true)
            {
                menu();
                Console.WriteLine("请输入一个运算符（输入0退出）");
                str = Console.ReadLine();
                if (str == "0")
                {
                    Environment.Exit(0);
                }
                try
                {
                    if (str == "+" || str == "-" || str == "/" || str == "*" || str == "%")
                    {
                        canshu1();
                        canshu2();
                        jisuan(str);
                        Console.WriteLine("结果是:");
                        num3 = Math.Round(num3, 2);
                        Console.WriteLine("<{0}> {1} <{2}> = {3}", num1, str, num2, num3);
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("你输入的有错请重新输入");
                }
                finally
                {
                    Console.WriteLine("按回车继续......");
                    Console.ReadLine();
                    zhixing();
                }
            }
        }
        static void Main(string[] args)
        {
            zhixing();
        }
    }
}
